---
title: 'Few-shot Prompting'
description: 'Learn how to implement few-shot learning with examples to improve AI performance on specific tasks'
---

## What is Few-shot Prompting?

Few-shot learning is a prompting technique where you provide the AI with one or several small number of examples (typically 1-10) to demonstrate the desired pattern, format, or behavior before asking it to perform a similar task. This technique leverages the AI's ability to recognize patterns and generalize from limited examples.

## Why Use Few-shot Prompting?

- **Improved Accuracy**: Examples help the AI understand exactly what you want
- **Consistent Format**: Ensures outputs follow a specific structure
- **Reduced Ambiguity**: Clear examples eliminate guesswork
- **Better Context Understanding**: Shows the AI how to handle edge cases
- **Domain Adaptation**: Helps AI adapt to specific domains or styles

## Zero-shot vs Few-shot
Zero-shot prompting involves asking the AI to perform a task without any examples, relying solely on its pre-existing knowledge. Few-shot prompting, on the other hand, provides a few examples to guide the AI's response. Few-shot learning is generally more effective for complex tasks where context and specific patterns are crucial.

## One-shot vs Few-shot
One-shot prompting provides a single example to guide the AI, the idea behind one-shot learning is to show the AI how to perform a task with just one example.

<Note>
All variants of few-shot prompting (zero-shot, one-shot, and few-shot) can be implemented in Latitude. The choice depends on the complexity of the task and the amount of guidance needed.
</Note>

## Basic Implementation in Latitude

Here's a simple few-shot learning example for email classification:

```markdown Email Classification
---
provider: OpenAI
model: gpt-4o
temperature: 0.1
---

# Email Classification Task

You are an expert email classifier. Based on the examples below, classify the given email
into one of these categories: URGENT, SPAM, BUSINESS, PERSONAL.

## Examples:

<user>
**Email**: "CONGRATULATIONS! You've won $1,000,000! Click here now!"
</user>
<assistant>
**Category**: SPAM
</assistant>

<user>
**Email**: "Hi John, can we reschedule tomorrow's meeting? I have a family emergency."
</user>
<assistant>
**Category**: PERSONAL
</assistant>

<user>
**Email**: "SYSTEM ALERT: Server down. Immediate attention required. Revenue impact critical."
</user>
<assistant>
**Category**: URGENT
</assistant>

<user>
**Email**: "Please find attached the Q4 financial report for your review."
</user>
<assistant>
**Category**: BUSINESS
</assistant>

## Email to Classify:
{{ email_content }}

## Category:
```

## Advanced Implementation with Variables

Let's create a more sophisticated example that uses [Latitude's parameters system](/guides/prompt-manager/playground#parameter-types):

<CodeGroup>
```markdown Advanced Few-shot
---
provider: OpenAI
model: gpt-4o
temperature: 0.2
---

# {{ product_category }} Product Review Sentiment Analysis

Analyze the sentiment of {{ product_category }} reviews and extract {{ detail_level }} insights. Use the examples below as a guide.

## Examples:

{{ for example in examples }}
**Review**: "{{ example.text }}"
**Sentiment**: {{ example.sentiment }}
**Key Points**: {{ example.key_points }}
**Confidence**: {{ example.confidence }}
{{ endfor }}

## Review to Analyze:
{{ review_text }}

## Analysis:
**Sentiment**:
**Key Points**:
**Confidence**:
```
</CodeGroup>

In this advanced example:

1. **Dynamic Content**: We use templates (`{{ variable }}`) to insert parameters into the prompt.

3. **Templating Features**: We demonstrate control structures like `{{for item in items }}` for arrays.

4. **Runtime Examples**: The `examples` array parameter allows users to pass in any number of examples when calling the prompt.

This pattern makes your prompts more flexible and reusable across different use cases without creating separate prompts for each scenario. For more on parameters, see the [Latitude Parameter Types documentation](/guides/prompt-manager/playground#parameter-types).

## Multi-step Few-shot with Chains

Latitude's chain feature allows you to create complex few-shot workflows:

<CodeGroup>
````markdown Multi-step Analysis
---
provider: OpenAI
model: gpt-4o
temperature: 0.1
---

<step>
# Information Extraction

Extract structured information from the business proposal. Follow these examples:

## Examples:

**Input**: "We propose a mobile app for food delivery in NYC with $50K funding needed."
**Output**:
{
  "business_type": "mobile app",
  "industry": "food delivery",
  "location": "NYC",
  "funding_needed": "$50K"
}

**Input**: "Looking for $2M Series A for our AI-powered healthcare platform serving hospitals nationwide."
```json
{
  "business_type": "AI platform",
  "industry": "healthcare",
  "location": "nationwide",
  "funding_needed": "$2M"
}
```

## Proposal to Extract:
{{ proposal_text }}

## Extracted Information:
</step>

<step>
# Business Viability Analysis

Based on the extracted information, provide a viability score following these examples:

## Examples:

**Business**: AI healthcare platform, $2M Series A, nationwide
**Viability Score**: 8.5/10
**Reasoning**: Large addressable market, proven demand, appropriate funding level

**Business**: Food delivery app, $50K seed, NYC only
**Viability Score**: 6.0/10
**Reasoning**: Competitive market, limited geographic scope, low initial funding

## Analysis:
**Viability Score**:
**Reasoning**:
</step>
````
</CodeGroup>

## Dynamic Few-shot with Conditional Logic

Use Latitude's conditional features to adapt examples based on context:

<CodeGroup>
```markdown Dynamic Few-shot
---
provider: OpenAI
model: gpt-4o
temperature: 0.3
---

# Content Generation with Context-Aware Examples

Generate {{ content_type }} following the appropriate examples below:

{{ if content_type === "social_media" }}
  <assistant>
  ## Social Media Examples:

  **Topic**: "New restaurant opening"
  **Content**: "🍕 EXCITING NEWS! Our new pizza place opens tomorrow at 123 Main St! First 50 customers get 50% off! #GrandOpening #Pizza #Foodie"

  **Topic**: "Product launch"
  **Content**: "🚀 Introducing our game-changing productivity app! Streamline your workflow like never before. Download now: [link] #ProductLaunch #Productivity #Innovation"
  </assistant>
{{ endif }}

{{ if content_type === "email" }}
  <assistant>
  ## Email Examples:

  **Topic**: "Welcome new customer"
  **Subject**: "Welcome to [Company] - Your journey starts here!"
  **Content**: "Dear [Name], Thank you for choosing [Company]. We're excited to help you achieve your goals. Here's what to expect next..."

  **Topic**: "Product announcement"
  **Subject**: "Introducing [Product] - Transform your [Industry]"
  **Content**: "Hi [Name], We're thrilled to announce [Product], designed specifically for professionals like you who want to [benefit]..."
  </assistant>
{{ endif }}

{{ if content_type === "blog" }}
  <assistant>
  ## Blog Examples:

  **Topic**: "Industry trends"
  **Content**: "# The Future of [Industry]: 5 Trends to Watch in 2024\n\nThe [industry] landscape is evolving rapidly. Here are the key trends shaping our future:\n\n## 1. [Trend Name]\n[Detailed explanation...]"

  **Topic**: "How-to guide"
  **Content**: "# Step-by-Step Guide: How to [Action]\n\nAre you struggling with [problem]? This comprehensive guide will walk you through everything you need to know.\n\n## Prerequisites\n- [Requirement 1]\n- [Requirement 2]"
  </assistant>
{{ endif }}

## Content Request:
**Topic**: {{ topic }}
**Target Audience**: {{ audience }}
**Key Message**: {{ key_message }}

## Generated {{ content_type }}:
```
</CodeGroup>

## Few-shot with Agent Collaboration

Combine few-shot learning with Latitude's agent system for complex workflows:

<CodeGroup>
```markdown Multi-Agent Few-shot
---
provider: OpenAI
model: gpt-4o
temperature: 0.2
type: agent
agents:
  - agents/data_extractor
  - agents/validator
  - agents/formatter
---

# Document Processing Pipeline

Process the document using specialized agents, each with their own few-shot examples.

## Document Input:
{{ document_content }}

## Processing Steps:

1. **Data Extraction**: Use the data_extractor agent to pull key information
2. **Validation**: Use the validator agent to verify accuracy
3. **Formatting**: Use the formatter agent to structure output

Process the document and provide the final structured result.
```

```markdown agents/data_extractor
---
provider: OpenAI
model: gpt-4o
temperature: 0.1
type: agent
---

# Data Extraction Specialist

Extract key data points from documents following these examples:

## Examples:

**Document**: "John Smith, Software Engineer at TechCorp, joined January 2023, salary $95,000"
**Extracted Data**:
- Name: John Smith
- Position: Software Engineer
- Company: TechCorp
- Start Date: January 2023
- Salary: $95,000

**Document**: "Meeting scheduled for March 15th, 2024 at 2:00 PM in Conference Room A with the Marketing team"
**Extracted Data**:
- Event Type: Meeting
- Date: March 15th, 2024
- Time: 2:00 PM
- Location: Conference Room A
- Attendees: Marketing team

## Document to Process:
{{ document_content }}

## Extracted Data:
```
</CodeGroup>

## Best Practices for Few-shot Prompting

<AccordionGroup>
<Accordion title="Example Selection">
**Choose Representative Examples**:
- Cover different scenarios and edge cases
- Include both positive and negative examples
- Ensure examples match your target domain
- Use diverse input formats when applicable

**Example Quality**:
- Make examples clear and unambiguous
- Include enough detail without being verbose
- Show consistent formatting patterns
- Demonstrate the reasoning process when needed
</Accordion>

<Accordion title="Prompt Structure">
**Optimal Structure**:
1. **Task Description**: Clear explanation of what you want
2. **Examples Section**: 2-10 well-chosen examples
3. **Input Section**: Where the new data goes
4. **Output Section**: Where the response should go

**Formatting Tips**:
- Use consistent separators between examples
- Clearly label input and output sections
- Include field names for structured outputs
- Use markdown formatting for readability
</Accordion>

<Accordion title="Variable Integration">
**Dynamic Examples**:
- Use Latitude variables to customize examples
- Implement conditional logic for context-aware examples
- Store example sets in prompt references for reusability
- Allow users to provide their own examples when needed

**Example with Variables**:
```markdown
{{ for example in examples }}
  **Input**: {{ example.input }}
  **Output**: {{ example.output }}
{{ endfor }}
```
</Accordion>

<Accordion title="Performance Optimization">
**Token Management**:
- Balance between example quantity and token efficiency
- Use the most informative examples
- Consider using shorter examples for simple tasks
- Cache common example sets using prompt references

**Model Selection**:
- Use more capable models (GPT-4) for complex few-shot tasks
- Consider fine-tuning for repeated patterns
- Adjust temperature based on creativity needs
- Test with different model sizes
</Accordion>
</AccordionGroup>

## Advanced Techniques

### Self-Improving Few-shot

Create prompts that can improve their own examples:

<CodeGroup>
```markdown Self-Improving
---
provider: OpenAI
model: gpt-4o
temperature: 0.4
---

<step>
# Process the task with current examples

Current examples:
{{ current_examples }}

Task: {{ user_task }}

Result:
</step>

<step>
# Evaluate the result quality

Result: {{ initial_processing }}

Was this result satisfactory? If not, what examples would improve future performance?

Evaluation:
</step>

<step>
# Generate improved examples if needed

Based on the evaluation. If needs improvement. Generate 2-3 new examples that would help with tasks like: {{ user_task }}
New examples should address the identified weaknesses.

Improved Examples:
</step>
```
</CodeGroup>

### Cross-Domain Transfer

Use few-shot learning to transfer patterns across domains:

<CodeGroup>
```markdown Cross-Domain Transfer
---
provider: OpenAI
model: gpt-4o
temperature: 0.3
---

# Cross-Domain Pattern Transfer

Learn the pattern from {{ source_domain }} examples and apply it to {{ target_domain }}.

## Source Domain Examples ({{ source_domain }}):

{{ for example in source_examples }}
**Input**: {{ example.input }}
**Pattern Applied**: {{ example.pattern }}
**Output**: {{ example.output }}
{{ endfor }}

## Pattern Recognition:
The consistent pattern across these examples is: {{ identified_pattern }}

## Target Domain Application ({{ target_domain }}):
Now apply the same pattern to this {{ target_domain }} scenario:

**Input**: {{ target_input }}
**Pattern to Apply**: {{ identified_pattern }}
**Output**:
```
</CodeGroup>

## Common Pitfalls and Solutions

<Warning>
**Avoid These Common Mistakes**:
- **Too Many Examples**: More isn't always better; 3-7 examples are usually optimal
- **Inconsistent Formatting**: Make sure all examples follow the same structure
- **Biased Examples**: Include diverse scenarios to avoid model bias
- **Unclear Boundaries**: Clearly separate examples from the actual task
</Warning>

<Tip>
**Pro Tips**:
- Start with 2-3 examples and add more if needed
- Test your few-shot prompts with edge cases
- Use Latitude's version control to iterate on example sets
- Combine with other techniques like Chain-of-Thought for complex reasoning
</Tip>

## Next Steps

Now that you understand few-shot learning, explore these related techniques:

- [Chain-of-Thought](/examples/techniques/chain-of-thought) - Add reasoning steps to your examples
- [Template-based Prompting](/examples/techniques/template-based-prompting) - Structure your few-shot examples
- [Role Prompting](/examples/techniques/role-prompting) - Combine examples with specific roles
- [Self-Consistency](/examples/techniques/self-consistency) - Use multiple few-shot attempts for better results
